(function() {
    'use strict';

    /**
     * @ngdoc function
     * @region webApp.controller:DesktypeCtrl
     * @description
     * # DesktypeCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('DesktypeCtrl', DesktypeCtrl);

    DesktypeCtrl.$inject = ['$scope', 'dataService', '$timeout', '$location', 'helper', '$document', '$filter', '$state', '$interval', '$rootScope'];


    function DesktypeCtrl($scope, dataService, $timeout, $location, helper, $document, $filter, $state, $interval, $rootScope) {


        //初始方法
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getlist();

        }

        //获取列表
        $scope.getlist = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : "1";
            var query = {
                content: {
                    page_no: $scope.pageNo,
                    page_size: $scope.pageSize,
                    store_id: $rootScope.store_id,
                }
            };

            dataService.tabletypeGet(query,1).then(function(list) {
                if (list.table_type_list) {
                    $scope.table_type_list = list.table_type_list;
                    $scope.tableTotal = list.total_count;
                }
            });
        }


        $scope.addAction = function() {
            $scope.tableid = $location.search().table_type_id;
            if ($scope.tableid) {
                $scope.getOne();
            } else {
                $scope.deskdata = { min_seat_num: 2, table_type_id: 0, max_seat_num: 4 };
            }
        }

        $scope.getOne = function() {
            dataService.tabletypeGet({ content: { table_type_id: $scope.tableid, store_id: $rootScope.store_id } }).then(function(list) {
                if (list.table_type_list) {
                    $scope.deskdata = list.table_type_list[0];
                }
            });
        }

        $scope.addData = function() {
            if ($scope.deskdata) {
                var data = {
                    content: {
                        "table_type_id": $scope.deskdata.table_type_id,
                        "min_seat_num": $scope.deskdata.min_seat_num,
                        "max_seat_num": $scope.deskdata.min_seat_num,
                        "name": $scope.deskdata.name,
                        "store_id": $rootScope.store_id
                    }
                }
                dataService.tabletypeAdd(data).then(function(re2) {
                    if (re2.table_type_id) {
                        helper.myAlert("success", null, null, function() {
                            $state.go("desktype.list");
                        });
                    } else {
                        helper.myAlert(re2.Msg);
                    }
                });
            }

        }

        $scope.Delete = function(id) {
            if (id) {
                helper.myConfirm("确认删除吗？", function() {
                    dataService.tabletypeDelete({ content: { table_type_id: id } }).then(function(re2) {
                        if (re2.table_type_id) {
                            helper.myAlert("success");
                            $scope.getlist();
                        } else {
                            helper.myAlert(re2.Msg);
                        }
                    });
                })

            }

        }

    }
})();